package com.zhonghui.mes.service.impl;

import com.zhonghui.common.utils.DateUtils;
import com.zhonghui.common.utils.StringUtils;
import com.zhonghui.mes.domain.MesJobAssembly;
import com.zhonghui.mes.domain.MesProductionCompletion;
import com.zhonghui.mes.domain.vo.MesJobAssemblyItemVo;
import com.zhonghui.mes.domain.vo.MesJobAssemblyVo;
import com.zhonghui.mes.domain.vo.MesPlannedProductionItemVo;
import com.zhonghui.mes.domain.vo.MesPlannedProductionVo;
import com.zhonghui.mes.mapper.MesJobAssemblyMapper;
import com.zhonghui.mes.mapper.MesPlannedProductionMapper;
import com.zhonghui.mes.mapper.MesProductionCompletionMapper;
import com.zhonghui.mes.service.IMesJobAssemblyService;
import com.zhonghui.mes.service.IMesPlannedProductionService;
import com.zhonghui.mes.service.IMesProductionCompletionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 生产作业Service业务层处理
 * 
 * @author zhonghui
 * @date 2022-05-21
 */
@Service
public class MesJobAssemblyServiceImpl implements IMesJobAssemblyService 
{
    @Autowired
    private MesJobAssemblyMapper mesJobAssemblyMapper;

    @Autowired
    private MesPlannedProductionMapper mesPlannedProductionMapper;

    @Autowired
    private MesProductionCompletionMapper mesProductionCompletionMapper;

    @Autowired
    private IMesProductionCompletionService mesProductionCompletionService;

    @Autowired
    private IMesPlannedProductionService mesPlannedProductionService;

    /**
     * 查询生产作业
     * 
     * @param id 生产作业主键
     * @return 生产作业
     */
    @Override
    public MesJobAssemblyVo selectMesJobAssemblyById(Long id)
    {
        return mesJobAssemblyMapper.selectMesJobAssemblyById(id);
    }

    /**
     * 查询生产作业列表
     * 
     * @param mesJobAssembly 生产作业
     * @return 生产作业
     */
    @Override
    public List<MesJobAssemblyVo> selectMesJobAssemblyList(MesJobAssembly mesJobAssembly)
    {
        return mesJobAssemblyMapper.selectMesJobAssemblyList(mesJobAssembly);
    }

    /**
     * 新增生产作业
     * 
     * @param mesJobAssemblyVo 生产作业
     * @return 结果
     */
    @Transactional
    @Override
    public int insertMesJobAssembly(MesJobAssemblyVo mesJobAssemblyVo)
    {
        int rows = mesJobAssemblyMapper.insertMesJobAssembly(mesJobAssemblyVo);
        insertMesJobAssemblyItem(mesJobAssemblyVo);
        return rows;
    }

    /**
     * 修改生产作业
     * 
     * @param mesJobAssemblyVo 生产作业
     * @return 结果
     */
    @Transactional
    @Override
    public int updateMesJobAssembly(MesJobAssemblyVo mesJobAssemblyVo)
    {
        mesJobAssemblyMapper.deleteMesJobAssemblyItemByJobId(mesJobAssemblyVo.getId());
        insertMesJobAssemblyItem(mesJobAssemblyVo);
        return mesJobAssemblyMapper.updateMesJobAssembly(mesJobAssemblyVo);
    }

    /**
     * 批量删除生产作业
     * 
     * @param ids 需要删除的生产作业主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteMesJobAssemblyByIds(Long[] ids)
    {
        mesJobAssemblyMapper.deleteMesJobAssemblyItemByJobIds(ids);
        return mesJobAssemblyMapper.deleteMesJobAssemblyByIds(ids);
    }

    /**
     * 删除生产作业信息
     * 
     * @param id 生产作业主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteMesJobAssemblyById(Long id)
    {
        mesJobAssemblyMapper.deleteMesJobAssemblyItemByJobId(id);
        return mesJobAssemblyMapper.deleteMesJobAssemblyById(id);
    }

    /**
     * 新增制造执行MES-生产作业明细信息
     * 
     * @param mesJobAssemblyVo 生产作业对象
     */
    public void insertMesJobAssemblyItem(MesJobAssemblyVo mesJobAssemblyVo)
    {
        List<MesJobAssemblyItemVo> mesJobAssemblyItemList = mesJobAssemblyVo.getMesJobAssemblyItemList();
        Long id = mesJobAssemblyVo.getId();
        if (StringUtils.isNotNull(mesJobAssemblyItemList))
        {
            List<MesJobAssemblyItemVo> list = new ArrayList<MesJobAssemblyItemVo>();
            for (MesJobAssemblyItemVo mesJobAssemblyItem : mesJobAssemblyItemList)
            {
                mesJobAssemblyItem.setJobId(id);
                list.add(mesJobAssemblyItem);
            }
            if (list.size() > 0)
            {
                mesJobAssemblyMapper.batchMesJobAssemblyItem(list);
            }
        }
    }

    /**
     * 修改生产作业
     *
     * @param mesJobAssemblyVo 生产作业
     * @return 结果
     */
    @Transactional
    @Override
    public int updateMesJobAssemblyStatus(MesJobAssemblyVo mesJobAssemblyVo)
    {
        Integer status = mesJobAssemblyVo.getStatus();
        MesJobAssemblyVo job = mesJobAssemblyMapper.selectMesJobAssemblyById(mesJobAssemblyVo.getId());
        job.setStatus(status);
        // 执行后更细排产细表的物料使用数量
        if (status == 1) {
            job.setStartTime(DateUtils.getNowDate());
            MesPlannedProductionVo plannedProduction = mesPlannedProductionMapper.selectMesPlannedProductionById(job.getPlannedProductionId());
            List<MesPlannedProductionItemVo> mesPlannedProductionItemList = plannedProduction.getMesPlannedProductionItemList();
            List<MesJobAssemblyItemVo> mesJobAssemblyItemList = job.getMesJobAssemblyItemList();
            for (MesPlannedProductionItemVo material : mesPlannedProductionItemList) {
                List<MesJobAssemblyItemVo> collect = mesJobAssemblyItemList.stream()
                        .filter(detail -> detail.getMaterialId().longValue() == material.getMaterialId().longValue())
                        .collect(Collectors.toList());
                if (collect != null && !collect.isEmpty()) {
                    MesJobAssemblyItemVo mesJobAssemblyItemVo = collect.get(0);
                    double usage = material.getUsedQuantity() == null ? 0 : material.getUsedQuantity();
                    double left = material.getResidueQuantity() == null ? 0 : material.getResidueQuantity();
                    double consume = mesJobAssemblyItemVo.getUsageQuantity() == null ? 0 : mesJobAssemblyItemVo.getUsageQuantity();
                    material.setResidueQuantity(left - consume);
                    material.setUsedQuantity(usage + consume);
                }
            }
            mesPlannedProductionService.updateMesPlannedProduction(plannedProduction);
        }
        // 完成后更细排产的生产数量
        if (status == 2) {
            job.setEndTime(DateUtils.getNowDate());
            MesPlannedProductionVo plannedProduction = mesPlannedProductionMapper.selectMesPlannedProductionById(job.getPlannedProductionId());
            double quantity = plannedProduction.getQuantityProduced() == null ? 0 : plannedProduction.getQuantityProduced();
            plannedProduction.setQuantityProduced(quantity + job.getQuantity());
            mesPlannedProductionService.updateMesPlannedProduction(plannedProduction);
            //生成完工单
            MesProductionCompletion mesProductionCompletion = new MesProductionCompletion();
            mesProductionCompletion.setJobId(job.getId());
            mesProductionCompletion.setMaterialId(job.getMaterialId());
            mesProductionCompletion.setCompletionDate(job.getEndTime());
            mesProductionCompletion.setQuantity(job.getQuantity());
            Date date = new Date();
            SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmssSSS");
            mesProductionCompletion.setBatchNumber("CPB" + format.format(date));
            mesProductionCompletion.setStatus(0);
            mesProductionCompletionService.insertMesProductionCompletion(mesProductionCompletion);
        }
        return mesJobAssemblyMapper.updateMesJobAssembly(job);
    }
}
